import java.util.PriorityQueue;
import java.util.concurrent.PriorityBlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-05-22
 * Time: 14:36
 */
class Solution6 {
    public int lastStoneWeight(int[] stones) {
        PriorityQueue<Integer> queue = new PriorityQueue<>((a,b)->b-a);

        for(int x : stones) {
            queue.offer(x);
        }

        int a = 0;

        while(queue.size() > 1) {
            a = queue.poll() - queue.poll();
            queue.offer(a);
        }

        return queue.isEmpty() ? 0 : queue.poll();
    }
}
